package com.atguigu.crmservice.controller;


import com.atguigu.commentutils.R;
import com.atguigu.crmservice.entity.CrmBanner;
import com.atguigu.crmservice.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-10-20
 */
@Api(description = "后台管理员管理")
@CrossOrigin
@RestController
@RequestMapping("/eduservice/banneradmin")
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    //分页查询banner
    @GetMapping("pageBanner/{page}/{limit}")
    @ApiOperation(value = "分页查询")
    public R pageBanner(@PathVariable long page,
                        @PathVariable long limit){
        Page<CrmBanner> pageBanner = new Page<>(page,limit);
        bannerService.page(pageBanner,null);
        return R.ok().data("total",pageBanner.getTotal()).data("items",pageBanner.getRecords());
    }

    //添加banner
    @PostMapping("addBanner")
    @ApiOperation(value = "添加banner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        boolean save = bannerService.save(crmBanner);
        if (save) return R.ok();
        else return R.error();
    }

    //修改Banner
    @PostMapping("update")
    @ApiOperation(value = "修改Banner")
    public R updateById(@RequestBody CrmBanner crmBanner){
        boolean b = bannerService.updateById(crmBanner);
        if(b) return R.ok();
        else return R.error();
    }

    //删除
    @DeleteMapping("remove/{id}")
    @ApiOperation(value = "删除Banner")
    public R remove(@PathVariable String id){
        boolean b = bannerService.removeById(id);
        if (b) return R.ok();
        else return R.error();
    }

    //根据ID查询
    @ApiOperation(value = "根据ID查询")
    @GetMapping("get/{id}")
    public R getByid(@PathVariable String id){
        CrmBanner byId = bannerService.getById(id);
        return R.ok().data("banner",byId);
    }
}

